package cn.chint.manage.dao.impl;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.chint.po.SysUser;
/**
 * @author admin
 */
@Repository
public class AdminDaoImpl implements cn.chint.manage.dao.AdminDao {
    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SysUser findChannelUser(final String username,final String password) {

		List<SysUser> ls  = (List<SysUser>) this.hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				return session.createQuery("from SysUser u where u.username=:username and u.password=:password")
						.setString("username", username).setString("password", password).list();

			}
		});

		if(ls.size()>0){
			SysUser sysUser = ls.get(0);
			return sysUser ;
		}else{
			return null ;
		}
    }

	@Override
	public void amend( final String password,final String username) {
		 this.hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				return session.createSQLQuery("update t_sys_user set password=:password where username=:username")
						.setString("password",password).setString("username",username).executeUpdate();
			}
		});
	}
}


















